package socket;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import service.MessageHandler;
import service.ServerAction;

public class ChatServerClientThread extends Thread {
	private Socket connection = null;
	private ObjectOutputStream out;
	private ObjectInputStream in;
	private Connection connecting;
	
	public ChatServerClientThread(Socket connection, ObjectOutputStream out, ObjectInputStream in) {
		this.connection = connection;
		this.out = out;
		this.in = in;
	}
	// [V] Add & remove connecting from list
	public synchronized void creatingRecordInList() {
		ChatServer.connections.add(connecting);
	}
	
	public synchronized void removingRecordFromList() {
		ChatServer.connections.remove(connecting);
	}
	
	public void run(){
		MessageHandler messageHandler = new MessageHandler(out, in);
		String message;
		
		try {
			System.out.println("Connection received from "
					+ connection.getInetAddress().getHostName());
			
			ServerAction serverAction = new ServerAction(out, in);
			
			connecting = new Connection(connection,out,in);
			creatingRecordInList();
			do {
				message = messageHandler.readMessage();
				ClientCommand command = ClientCommand.getType(message);
        	    switch (command) {
        	    case exit:
					serverAction.exit();
					break;
					
        	    case unknown:
        	    	serverAction.unknown();
        	    	break;
        	    // [V] I didn't test this command	
        	    case writemsg: {
        	    	String msg = null;
        	    	// [V] This string should be synchronized (I didn't do that).
        	    	// Possible it's necessary to create 2 synchronized methods for strings (one for true other for false) 
        	    	ChatServer.connections.get(ChatServer.connections.indexOf(connecting)).busy = true;
        	    	msg = serverAction.writemsg();
        	    	new MessageDeliver(connection, out, in, msg).start();
        	    	// [V] This string should be synchronized
        	    	ChatServer.connections.get(ChatServer.connections.indexOf(connecting)).busy = false;
        	    	break;
        	    }
        	    // [V] It doesn't work correct. Some problems with sending & receiving messages
        	    case readmsg: {
        	    	String s = null;
        	    	// [V] This string should be synchronized
        	    	ChatServer.connections.get(ChatServer.connections.indexOf(connecting)).busy = false;
        	    	messageHandler.sendMessage(" You are in reading mode. To exit write quit ");
        	    	//do {
        	    		sleep(100);
        	    		//if (in.available() != 0) {
        	    		//	System.out.println(in.available());
        	    		//	s = messageHandler.readMessage();
        	    		//}
        	    		s = messageHandler.readMessage();
        	    	//} while(!"quit".equals(s));
        	    	messageHandler.sendMessage(" You are in command mode ");
        	    	// [V] This string should be synchronized
        	    	ChatServer.connections.get(ChatServer.connections.indexOf(connecting)).busy = true;
        	    	break;
        	    }
        	    }
     
			} while (!message.equals("exit"));
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ioException) {
			ioException.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
		    e.printStackTrace();
		} 
		finally {
			try {
				in.close();
				out.close();
				connection.close();
			} catch (IOException ioException) {
				ioException.printStackTrace();
			} catch (NullPointerException e) {     	
				e.printStackTrace();
			}
		}
	}		
		
}
